home *** CD-ROM | disk | FTP | other *** search
/ MacTech 1 to 12 / MacTech-vol-1-12.toast / Source / MacTech® Magazine / Volume 12 - 1996 / 12.11 Nov 96 / Symantec Top Ten Code / SmartTextField.java < prev    next >
Encoding:
Text File  |  1996-10-24  |  2.1 KB  |  83 lines  |  [TEXT/R*ch]

  1. // SmartTextField.java
  2. // MacTech's Symantec Top Ten, November 1996
  3.  
  4. class Cell extends Observable implements Observer
  5. {
  6.     double  curValue;
  7.     double  oldValue;
  8.     double  delta;
  9.  
  10.     SmartTextField  theText;
  11.  
  12.     public Cell(SmartTextField  aText)
  13.     {
  14.         theText = aText;
  15.     }
  16.  
  17.     public void Update(Observable o, Object arg)
  18.     {
  19.         oldValue = curValue;    // We are using a model where the cell is both an 
  20.                             // observing and observed so if this method is being
  21.                             // called then a cell that this object        
  22.                             // observes has changed.
  23.         curValue += ((Cell)o).delta;    // calculate delta
  24.                                     
  25.         theText.setText(""+curValue);    // set the SmartTextField to the new Value
  26.         super.setChanged();
  27.         notifyObservers();
  28.         super.clearChanged();       
  29.     }
  30. }
  31.  
  32. class SmartTextField extends TextField
  33. {
  34.     Cell    theCell;
  35.  
  36.     public SmartTextField(String someText, int width)
  37.     {
  38.         super(someText, width);
  39.         theCell = new Cell(this);
  40.     }
  41.  
  42.     ////------------ handleEvent ------------/////
  43.     public boolean handleEvent(Event evt)
  44.     {
  45.         // when the cell gets the focus, save its value 
  46.         // so we can  check if it has changed when it loses the focus
  47.         if(evt.id == evt.GOT_FOCUS)
  48.         {
  49.             ((SmartTextField)evt.target).selectAll();
  50.             ((SmartTextField)evt.target).theCell.oldVal =                       
  51.                 Double.valueOf(((SmartTextField)evt.target)
  52.                 .getText()).doubleValue();
  53.             return false;
  54.         }
  55.         
  56.         //lost the focus check to see if the value changed and deal with it
  57.         if(evt.id == evt.LOST_FOCUS)
  58.         {
  59.             //get the value of the current cell
  60.             ((SmartTextField)evt.target).theCell.curVal = 
  61.                 Double.valueOf(((SmartTextField)evt.target)
  62.                 .getText()).doubleValue();
  63.  
  64.             //calculate the delta
  65.             ((SmartTextField)evt.target).theCell.delta =  
  66.                 ((SmartTextField)evt.target).theCell.curVal -  
  67.                 ((SmartTextField)evt.target).theCell.oldVal;
  68.  
  69.             //if the delta is non zero, i.e. the value was changed
  70.             if ( ((SmartTextField)evt.target).theCell.delta != 0 )
  71.             {
  72.                 ((SmartTextField)evt.target).theCell.setChanged();
  73.                 ((SmartTextField)evt.target).theCell.notifyObservers();
  74.                 return true;
  75.             } 
  76.             else  
  77.                 return false;
  78.         }
  79.         else
  80.             return false;
  81.     }
  82. }
  83.